#include "stdafx.h"
#include "Thread.h"

CSimpleThread::CSimpleThread(void)
{
	m_hThread = 0;
	m_bRunning = FALSE;
}

CSimpleThread::~CSimpleThread(void)
{

}

BOOL CSimpleThread::_Begin(void)
{
	m_hThread = (HANDLE)_beginthreadex(NULL, 0, &_ThreadFunc, this, 0, NULL);
	if(m_hThread == NULL)
		return FALSE;

	m_bRunning = TRUE;

	return true;
}

void CSimpleThread::Stop(void)
{
	m_bRunning = FALSE;
}

unsigned int CSimpleThread::_ThreadFunc(void* param)
{
	INT32 nRetCode = 0;
	CSimpleThread* thread = (CSimpleThread*)param;

	while(thread->IsRunning())
	{
		nRetCode = thread->Update();
		if(!nRetCode)
		{
			thread->OnTerminate();
			CloseHandle(thread->m_hThread);
			return -1;
		}
	}

	thread->OnTerminate();
	CloseHandle(thread->m_hThread);
	return 0;
}
